import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueDevTools from 'vite-plugin-vue-devtools';
import VueSetupExtend from 'vite-plugin-vue-setup-extend';
import AutoImport from 'unplugin-auto-import/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import IconsResolver from 'unplugin-icons/resolver';
import path from 'path';
import Components from 'unplugin-vue-components/vite';
import Icons from 'unplugin-icons/vite';
import Inspect from 'vite-plugin-inspect';
// https://vite.dev/config/
const pathSrc = fileURLToPath(new URL('./src', import.meta.url));
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    VueSetupExtend(),
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: ['vue'],
      resolvers: [
        // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
        ElementPlusResolver(),
        // 自动导入图标组件
        IconsResolver({
          prefix: 'Icon'
        })
      ],
      dts: path.resolve(pathSrc, 'auto-imports.d.ts')
    }),

    Components({
      resolvers: [
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ['ep']
        }),
        // 自动导入Element Plus组件
        ElementPlusResolver()
      ],
      dts: path.resolve(pathSrc, 'components.d.ts')
    }),
    Icons({
      autoInstall: true
    }),
    Inspect()
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    host: '0.0.0.0',
    port: 8001,
    proxy: {
      '/api': {
        target: 'http://101.37.105.53:8100',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, '')
      },
      '/image': {
        target: 'http://101.37.105.53:8100',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/imag/, '')
      },
      '/apDev': {
        target: 'http://127.0.0.1:8100',
        // target: 'http://101.37.105.53:8100',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/apDev/, '')
      },
      '/imaDev': {
        target: 'http://127.0.0.1:8100',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/imaDev/, '')
      }
    }
  }
});
